require "import"
import "tools"
activity.setContentView("handle_")
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS).setStatusBarColor(0xff4DB6AC)
more=false
function exe(context,command)
  require "import"
  import "java.io.*"
  local cmd=luajava.astable(String(tostring(context.getFilesDir()).."/ffmpeg "..command).split(" "))
  local process=Runtime.getRuntime().exec(cmd)
  local err=BufferedReader(InputStreamReader(process.getErrorStream()))
  function readError(err)
    require "import"
    import "java.io.*"
    local info=""
    line=err.readLine()
    while line~=nil do
      info=info.."\n"..line
      line=err.readLine()
      call("back","progress",line)
    end
    call("back","done",info)
    this.quit()
  end
  thread(readError,err)
end
function draw(a)
  tv.background=LuaDrawable(function(c,p,d)
    p.setAntiAlias(true)
    p.setStrokeWidth(4)
    p.setColor(0xff4DB6AC)
    p.setStyle(p.Style.FILL)
    c.drawArc(activity.getWidth()/4,activity.getWidth()/4,activity.getWidth()*3/4,activity.getWidth()*3/4, 0,a*3.6, true, p);
    p.setColor(0xff000000)
    p.setStyle(p.Style.STROKE)
    c.drawArc(activity.getWidth()/4,activity.getWidth()/4,activity.getWidth()*3/4,activity.getWidth()*3/4, 0,a*3.6, true, p);
    c.drawLine(activity.getWidth()/2,activity.getWidth()/2,activity.getWidth()*3/4,activity.getWidth()/2,p)
    p.setStyle(p.Style.STROKE)
    c.drawCircle(activity.getWidth()/2,activity.getWidth()/2,activity.getWidth()/4,p)
    p.setStyle(p.Style.FILL)
    p.setTextSize(50)
    if a<100
      c.drawText(math.floor(a).."%",activity.getWidth()/2-50,activity.getWidth()*9/10,p)
     else
      c.drawText("完成",activity.getWidth()/2-50,activity.getWidth()*9/10,p)
    end
  end)
end
function onDestroy()
  local f=File(LUA_SD.."/avHandle/tmp/")
  if f.exists()
    for k, v in ipairs(luajava.astable(f.listFiles()))
      os.remove(tostring(v))
    end
  end
end
cmd={...}
totalStep=#cmd
tip.text="任务执行步骤".."1/".. tostring(totalStep)
if #cmd>1
  more=true
end
for n=1,#cmd
  if cmd_tv.text==""
    cmd_tv.text=cmd_tv.text..cmd[n]
   else
    cmd_tv.text=cmd_tv.text.."\n\n"..cmd[n]
  end
end
copy.background=btBackground
copy. onClick=function()
  activity.getSystemService(Context.CLIPBOARD_SERVICE).setText(cmd_tv.text)
  print("已复制")
end
function back(statue,a)
  if statue=="progress"
    if a~=nil and a:find("Duration:")
      local s=a:match("Duration:(.-),")
      if not s:find("N/A")
        total=tonumber(String(s).split(":")[1])*60+tonumber(String(s).split(":")[2])
      end
    end
    if a~=nil and a:find("time=")
      local s=a:match("time=(.*) bitrate")
      local now=tonumber(String(s).split(":")[1])*60+tonumber(String(s).split(":")[2])
      if type(total)=="number"
        draw(100*now/total)
      end
    end
  end
  if statue=="done"
    draw(100)
    if more==true
      table.remove(cmd,1)
      if #cmd==1
        more=false
      end
      tip.text="任务执行步骤".. tostring(totalStep-#cmd).."/".. tostring(totalStep)
      exe(activity,cmd[1],"back")
     else
      print("已处理完成！")
      activity.finish()
    end
  end
end
exe(activity,cmd[1],"back")